package com.lzx.iteam;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.lzx.iteam.adapter.ImageAdpter;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.ImageLoaderInterface;
import io.rong.imageloader.core.listener.PauseOnScrollListener;

import java.util.ArrayList;


/**
 * 聊天选择图片，显示最近的图片
 * Created by zhangshuai on 15-2-27.
 */
public class SelectImageActivity extends FragmentActivity implements AdapterView.OnItemClickListener{


    private GridView mGridView;
    private ImageAdpter mAdapter;
    private Cursor mCursor;
    private ArrayList<String> mImgePathList;
    //private ImageLoader imageLoader = ImageLoader.getInstance();

    public static  final  int SELECT_IMAGE = 2301;
    public static  final  int BACK_WITHOUT = 2302;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recent_image);
        mGridView = (GridView) findViewById(R.id.recent_image);
        mImgePathList = new ArrayList<String>();
        onPrepare();
        //imageLoader.init(ImageLoaderConfiguration.createDefault(this));
        mAdapter = new ImageAdpter(this,mImgePathList);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(this);
    }

    public void onPrepare(){
        String[] proj = { MediaStore.Images.Media.DATA };
        Uri mUri = Uri.parse("content://media/external/images/media");
        mCursor = managedQuery(mUri, proj, null, null, "_id desc");
        while(mCursor.moveToNext()){
            int column_index = mCursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            String fileName = mCursor.getString(column_index);
            mImgePathList.add(fileName);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(this,ImagePagerActivity.class);
        intent.putExtra(Constants.IMAGE_PATH,mImgePathList.get(i));
        intent.putExtra(Constants.IMAGE_ORIGIN,Constants.IMAGE_ORIGIN_LOCAL_STRING);
        startActivityForResult(intent,SELECT_IMAGE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        applyScrollListener();
    }

    /**
     * 滑动的过程中不加载图片，滑动停止开始加载
     */
    private void applyScrollListener() {
        mGridView.setOnScrollListener(new PauseOnScrollListener(ImageLoaderInterface.imageLoader,  false, true));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        setResult(BACK_WITHOUT);
        this.finish();
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if( resultCode == RESULT_OK){
            switch (requestCode){
                case SELECT_IMAGE:
                    String path = data.getStringExtra(Constants.IMAGE_PATH);
                    Intent intent = new Intent();
                    intent.putExtra(Constants.IMAGE_PATH,path);
                    setResult(RESULT_OK,intent );
                    finish();
                    break;
            }
        }


    }
}
